import { Column, Entity, ManyToOne, OneToMany, Index } from 'typeorm';
import { BaseEntity } from '../../../common/entities/base.entity';
import { MasterCategory } from './master-category.entity';
import { NovelChapter } from './novel-chapter.entity';
import { Comment } from './comment.entity';
import { UserFavorite } from './user-favorite.entity';
import { UserReadingHistory } from './user-reading-history.entity';

export enum NovelStatus {
  OFFLINE = 0,
  SERIAL = 1,
  COMPLETED = 2,
}

@Entity('novels')
@Index(['title'])
@Index(['author'])
@Index(['masterCategoryId'])
@Index(['status'])
@Index(['ratingScore'])
export class Novel extends BaseEntity {
  @Column({ type: 'varchar', length: 200, comment: '小说标题' })
  title: string;

  @Column({ type: 'varchar', length: 100, comment: '作者' })
  author: string;

  @Column({ type: 'text', nullable: true, comment: '小说简介' })
  description: string | null;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '封面图片URL',
  })
  coverImage: string | null;

  @Column({ type: 'bigint', nullable: true, comment: '母分类ID' })
  masterCategoryId: number | null;

  @Column({
    type: 'tinyint',
    default: NovelStatus.SERIAL,
    comment: '状态: 0-下架 1-连载 2-完结',
  })
  status: NovelStatus;

  @Column({ type: 'int', default: 0, comment: '总字数' })
  wordCount: number;

  @Column({ type: 'int', default: 0, comment: '阅读次数' })
  readCount: number;

  @Column({ type: 'int', default: 0, comment: '收藏次数' })
  favoriteCount: number;

  @Column({
    type: 'decimal',
    precision: 3,
    scale: 2,
    default: 0.0,
    comment: '评分',
  })
  ratingScore: number;

  @Column({ type: 'int', default: 0, comment: '评分人数' })
  ratingCount: number;

  @Column({ type: 'json', nullable: true, comment: '小说标签' })
  tags: string[] | null;

  @Column({ type: 'varchar', length: 50, nullable: true, comment: '来源平台' })
  platform: string | null;

  @Column({
    type: 'varchar',
    length: 1500,
    nullable: true,
    comment: '原始链接',
  })
  originalUrl: string | null;

  // 关联关系
  @ManyToOne(() => MasterCategory)
  masterCategory: MasterCategory;

  @OneToMany(() => NovelChapter, (chapter) => chapter.novel)
  chapters: NovelChapter[];

  @OneToMany(() => Comment, (comment) => comment.novel)
  comments: Comment[];

  @OneToMany(() => UserFavorite, (favorite) => favorite.novel)
  favorites: UserFavorite[];

  @OneToMany(() => UserReadingHistory, (history) => history.novel)
  readingHistory: UserReadingHistory[];
}
